﻿using System;
using System.Collections.Generic;
using System.Text;
using WordService.Business.IBLL;
using WordLib.Model;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
using WordService.DBUtil;
using System.Data;

namespace WordService.Business.BLL
{
    public sealed class FileBLL : IFileBLL
    {
        // 正则表达式
        private const String regexStr = @"\d{1,2}/\d{2}";
        private Dictionary<String, String> patterns = new Dictionary<String, String>();

        public FileBLL()
        {
            // 提取某一项属性
            patterns.Add("题", "Title");
            patterns.Add("作", "Author");
            patterns.Add("机", "Institution");
            patterns.Add("刊", "FullName");
            patterns.Add("ISSN", "ISSN");
            patterns.Add("C", "CN");
            patterns.Add("馆", "Collect");
            patterns.Add("关", "Keyword");
            patterns.Add("分", "Category");
            patterns.Add("文", "Summary");
        }

        public IList<InputDataModel> GetFileList(FileInfo file)
        {
            IList<InputDataModel> resultList = new List<InputDataModel>();
            using (StreamReader sr = new StreamReader(file.FullName, Encoding.UTF8))
            {
                String line;
                InputDataModel model = null;
                //Boolean setAttr = false;
                line = sr.ReadLine();
                do
                {
                    Regex regex = new Regex(regexStr);
                    if (regex.IsMatch(line))
                    {
                        if (model != null)
                        {
                            resultList.Add(model);
                        }
                        model = new InputDataModel();
                    }
                    // 赋值
                    if (model != null)
                    {
                        InjectAttr(line, model);
                    }
                } while ((line = sr.ReadLine()) != null);
                // 最终还需要添加一个进来
                resultList.Add(model);
            }
            return resultList;
        }

        private void InjectAttr(String inputLine, InputDataModel model)
        {
            Type type = model.GetType();
            String[] keyValuePair = inputLine.Split('】');
            if (keyValuePair.Length == 2)
            {
                foreach (String key in patterns.Keys)
                {
                    if (keyValuePair[0].Contains(key))
                    {
                        String value = "";
                        patterns.TryGetValue(key, out value);
                        type.GetProperty(value).SetValue(model, keyValuePair[1], null);
                    }
                }
            }
        }
        /// <summary>
        /// 获取Xml的配置文件
        /// </summary>
        /// <returns></returns>
        private XmlDocument GetConfigXml()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("RightRate.xml");
            return doc;
        }
        /// <summary>
        /// 获取关键字对应的权值表
        /// </summary>
        /// <returns></returns>
        public IDictionary<String, Decimal> GetKeywordRate()
        {
            IDictionary<String, Decimal> resultMap = new Dictionary<String, Decimal>();
            XmlDocument doc = GetConfigXml();
            XmlNodeList nodeList = doc.SelectNodes("RightRate/Keyword/Map");
            foreach (XmlNode node in nodeList)
            {
                resultMap.Add(new KeyValuePair<String,Decimal>(
                    node.Attributes["name"].Value,
                    Decimal.Parse(node.Attributes["value"].Value)));
            }
            return resultMap;
        }

        public Boolean SaveKeywordRate(IDictionary<String, Decimal> rateMap)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 获取作者对应的权值表
        /// </summary>
        /// <returns></returns>
        public IDictionary<Int32, Decimal> GetAuthorRate()
        {
            IDictionary<Int32, Decimal> resultMap = new Dictionary<Int32, Decimal>();
            XmlDocument doc = GetConfigXml();
            XmlNodeList nodeList = doc.SelectNodes("RightRate/Author/Map");
            foreach (XmlNode node in nodeList)
            {
                resultMap.Add(new KeyValuePair<Int32, Decimal>(
                    Int32.Parse(node.Attributes["name"].Value),
                    Decimal.Parse(node.Attributes["value"].Value)));
            }
            return resultMap;
        }

        public Boolean SaveAuthorRate(IDictionary<Int32, Decimal> rateMap)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 越过数据层的方法
        /// </summary>
        /// <returns></returns>
        public bool ClearData()
        {
            StringBuilder sqlStr = new StringBuilder();
            sqlStr.Append("DELETE FROM BAS_ARTICLE;");
            sqlStr.Append("DELETE FROM BAS_AUTHOR;");
            sqlStr.Append("DELETE FROM BAS_INSTITUTION;");
            sqlStr.Append("DELETE FROM BAS_KEYWORD;");
            sqlStr.Append("DELETE FROM REL_AUTHOR_INSTITUTION;");
            sqlStr.Append("DBCC CHECKIDENT(BAS_ARTICLE,RESEED,0);");
            sqlStr.Append("DBCC CHECKIDENT(BAS_AUTHOR,RESEED,0);");
            sqlStr.Append("DBCC CHECKIDENT(BAS_INSTITUTION,RESEED,0);");
            sqlStr.Append("DBCC CHECKIDENT(BAS_KEYWORD,RESEED,0);");
            sqlStr.Append("DBCC CHECKIDENT(REL_AUTHOR_INSTITUTION,RESEED,0);");
            try
            {
                DBUtility.ExecuteNonQuery(DBUtility.ConnectionStr, CommandType.Text, sqlStr.ToString(), null);
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
    }
}
